import React, {Component} from 'react';
import './index.less';
import {base, high, print, formDownload} from 'nc-lightapp-front';
import exportRosterInfo from "../../container/functions/exportRosterInfo";
import ReferSearch from "../../../../../../hrpub/common/components/referSearch/org";
import exportHtmlFunc from "../../../../../../hrpub/common/utils/exportHtml";
import Refer from '../../../../../refer/hiref/ReportDefineGridRef';
import do_print from "../../../../../../hrpub/common/utils/print";
import setQueryMeta from "../../container/functions/setQueryMeta";

class Header extends Component {
    constructor(props) {
        super(props);
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
        this.updateRefValue = this.updateRefValue.bind(this);
    }

    updateOrgValue(value) {
        this.props.updateOrgValue({
            orgVal: value
        });
    }

    updateRefValue(value) {
        this.props.updateRefValue({
            rosterRefVal: value
        });
    }

    onBtnOperation(props, btncode, areacode, opt) {
        switch (btncode) {
            case 'query':
                setQueryMeta.call(this);
                this.props.search.openAdvSearch('query', true);
                break;
            case 'sort':
                this.props.updateState({
                    sortModalVisible: true
                });
                break;
            case 'refresh':
                if (this.props.hasSearched) {
                    this.props.updateState({
                        pageInfo: {
                            ...this.props.pageInfo,
                            pageIndex: 1
                        }
                    }, () => {
                        this.props.getMainTableData();
                    });
                }
                break;
            case 'cardrpt':
                this.props.viewCard();
                break;
            case 'export':
                exportRosterInfo.call(this, 'cur');
                break;
            case 'exportall':
                exportRosterInfo.call(this, 'all');
                break;
            case 'print':
                do_print(document.querySelector('.main-table'),
                    {
                        title: this.props.json['hi6007-000180'],
                        maker: this.props.json['hi6007-000188'],
                        date: this.props.json['hi6007-000189']
                    }, {
                        tableTmp: this.props.meta.getMeta()['rosterTable'],
                        data: this.props.table.getAllTableData('rosterTable').rows
                    });
                break;
            case 'output':
                exportHtmlFunc(document.querySelector('.main-table'), {
                    title: this.props.json['hi6007-000180'],
                    fileName: this.props.json['hi6007-000180'],
                    maker: this.props.json['hi6007-000188'],
                    date: this.props.json['hi6007-000189']
                }, {
                    meta: this.props.meta.getMeta()['rosterTable'],
                    data: this.props.table.getAllTableData('rosterTable').rows,
                    showIndex: true
                });
                break;
            default:
                break;
        }
    }

    render() {
        const {button, orgVal, rosterRefVal} = this.props;

        return (
            <div className="header">
                <div className="header-refer">
                    <ReferSearch
                        orgVal={orgVal}
                        getOrgData={this.updateOrgValue}
                    />
                </div>

                <div className='roster-refer'>
                    <Refer queryCondition={{pk_org: orgVal.refpk}}
                           value={rosterRefVal}
                           onChange={this.updateRefValue}/>
                </div>

                <div className="btn-group">
                    <div className="definition-icons">
                        {button.createButtonApp({
                            area: 'head',
                            onButtonClick: (props, btncode) => {
                                this.onBtnOperation(props, btncode, 'head', this);
                            }
                        })}
                    </div>
                </div>
            </div>
        );
    }
}

export default Header;
